import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {
  CpuCodeDocumentType,
  CpuSeriesDocumentType,
  CpuCodeInfoDocumentType,
} from '../schema/CPU/cpu_code_schema';
// import { CreateCpuDto } from './dto/create-cpu.dto';
// import { UpdateCpuDto } from './dto/update-cpu.dto';

@Injectable()
export class CpuService {
  constructor(
    @InjectModel('CPU_CODE') private CpuCodeModel: Model<CpuCodeDocumentType>,
    @InjectModel('CPU_Series')
    private CpuSeriesModel: Model<CpuSeriesDocumentType>,
    @InjectModel('CPU_CODE_INFO')
    private CpuModel: Model<CpuCodeInfoDocumentType>,
  ) {}

  async createCpuSeries(manufacturers: string, series: string) {
    const cpu_series = new this.CpuSeriesModel({
      manufacturers,
      series,
    });
    // console.log(cpu_series);
    return cpu_series.save();
  }

  async getCpuSeries(manufacturers: string) {
    const result = await this.CpuSeriesModel.find({ manufacturers }).exec();
    return result;
  }

  async createCpuCode(
    manufacturers: string,
    series: string,
    code_name: string,
  ) {
    const cpu_code = new this.CpuCodeModel({
      manufacturers,
      series,
      code_name,
    });
    console.log(cpu_code);
    return cpu_code.save();
  }

  async getCpuCode(manufacturers: string, series: string) {
    const result = await this.CpuCodeModel.find({
      manufacturers,
      series,
    }).exec();
    // console.log(result);

    return result;
  }

  async createCpuInfo(body: any) {
    const cpu = new this.CpuModel(body);
    return cpu.save();
  }

  async getCpuInfo(query: any) {
    const { page, limit, series, socket_type, price, order } = query;

    let sort = {};
    if (order) {
      sort = { price: parseInt(order) };
    } else {
      sort = {};
    }

    // 构建查询条件
    const filter: any = {};
    if (series) {
      filter.series = series;
    }
    if (socket_type) {
      filter.socket_type = socket_type;
    }
    if (price) {
      if (price.min && !price.max) {
        filter.price = {
          $gte: parseFloat(price.min),
        };
      } else if (price.max && !price.min) {
        filter.price = {
          $lte: parseFloat(price.max),
        };
      } else {
        filter.price = {
          $gte: parseFloat(price.min),
          $lte: parseFloat(price.max),
        };
      }
    }
    // 执行查询
    const result = await this.CpuModel.find(filter)
      .skip((page - 1) * limit)
      .limit(limit)
      .sort(sort)
      .exec();
    return {
      code: 200,
      msg: 'success',
      data: {
        result,
        total: await this.CpuModel.countDocuments(filter),
      },
    };
  }

  async getCpuInfoById(id: string) {
    const result = await this.CpuModel.findById(id).exec();
    return {
      code: 200,
      msg: 'success',
      data: result,
    };
  }

  async updateCpuInfo(id: string, body: any) {
    const result = await this.CpuModel.findByIdAndUpdate(
      id,
      body as CpuCodeInfoDocumentType,
      { new: true },
    ).exec();
    return {
      code: 200,
      msg: 'success',
      data: result,
    };
  }

  async deleteCpuInfo(id: string) {
    await this.CpuModel.findByIdAndDelete(id).exec();
    return {
      code: 200,
      msg: '删除成功',
    };
  }

  async getCpuInfoTotal() {
    // 限制返回的字段 price score id
    const result = await this.CpuModel.find().select('price score id').exec();
    return {
      code: 200,
      msg: 'success',
      data: result,
    };
  }
}
